<template>
  <view class="vmall-address">
    <view v-if="addressList.length > 0" class="container">
      <view v-for="(item, index) in addressList" :key="index" class="list px-20 py-20" @click.stop="onCheckAddress(item)">
        <view v-if="type=='cart'" style="width: 50rpx;">
          <image class="icon-width mr-20" v-if="item.isDefault" :src="IMG_URL2+'login/checkbox1.svg'"></image>
          <image class="icon-width mr-20" v-else :src="IMG_URL2+'login/checkbox.svg'"></image>
        </view>
        <view class="cell pb-20 mid">
          <view class="f-28-B mb-8 flex items-center">
            <view>{{ item.shopName }}</view>
            <view v-if="item.isDefault" class="moren">默认</view>
          </view>
          <view class="f-24 mb-8">
            <text>{{ item.province }}</text>
            <text>{{ item.city }}</text>
            <text>{{ item.street }}</text>
            <text>{{ item.detailAddress }}</text>
          </view>
          <view class="flex f-24">
            <text>{{ item.name }}</text>
            <text style="margin-left: 10rpx;">{{ item.phone }}</text>
          </view>
        </view>
        <view class="flex justify-between mt-20 f-28 w-100">
          <view class="flex flex1 justify-end">
            <view class="flex items-center justify-center mr-28" @click.stop="onAddressEdit(item)">
              <text>编辑</text>
            </view>
            <!-- <view class="flex items-center justify-end" @click.stop="onAddressDelete(item, index)">
              <text>删除</text>
            </view> -->
          </view>
        </view>
      </view>
      <up-divider v-if="addressList.length>pageSize"  :text="loadStatus"></up-divider>
    </view>
    <view v-else class="empty-container flex justify-center items-center">
      <Empty :emptyInfo="emptyInfo" />
    </view>
    <view class="btn-fixed flex justify-center px-32 py-20">
      <view class="save-btn flex justify-center f-32-B white" @click.stop="onAddressAdd">
        <text>添加收货地址</text>
      </view>
    </view>
  </view>
</template>

<script setup>
import { IMG_URL2 } from "@/config";
import Empty from "@/components/empty.vue";
import { shopApi } from "@/api";

let pageSize = ref(10)
let currentPage = ref(1)
let totalCount = ref(0)
let loadStatus =ref('正在加载中')
let isloading = ref(false)

const EmptyImage = ref(IMG_URL2 + "empty/normal.png");
const addressList = ref([]);

const emptyInfo = ref({
  image: EmptyImage,
  width: 300,
  height: 300,
  tip: "还没有添加地址哦，快来添加吧~"
});
const type = ref('');

onLoad((options) => {
  console.log(options);
  type.value = options.type;
});

onShow(() => {
  getData(1,()=>{});
});



const onCheckDefault = () => {
  form.isDefault = form.isDefault == true ? false : true;
};

// 选择收货地址
const onCheckAddress = (item) => {
  if (type.value) {
    uni.$emit("check-address", item);
    uni.navigateBack();
  }
};

// 初始化请求数据
const getData = (type,cb) => {
  let data={
    currentPage: currentPage.value,
    pageSize: pageSize.value
  }
  isloading.value=true
  shopApi.SHOPRECEIVEGETLIST(data).then((res) => {
    isloading.value=false
    if(res.code==200){
      totalCount.value=res.result.totalCount
      if(type==1){
        cb && cb()
        addressList.value=res.result.data
      }else{
        addressList.value=res.result.data?addressList.value.concat(res.result.data):[]
      }
    }else{
      uni.showToast({
          title: res.errMsg,
          icon: "success",
      });
  }
    
  })
};

// 修改默认地址
const onAddressDefault = (item) => {
  addressList.value.forEach((item) => {
    item.isDefault = false;
  });
  item.isDefault = 2;
  shopApi.SHOPRECEIVEGSAVEORUPDATE({
      id: item.id
    }).then((res) => {
      if (res.code == 200) {
        uni.showToast({
          title: "设置成功",
          icon: "success",
          mask: true
        });
      }else{
      uni.showToast({
          title: res.errMsg,
          icon: "success",
      });
  }
    });
};
// 添加地址
const onAddressAdd = () => {
  uni.navigateTo({ url: "/pages/user/address/add" });
};
// 编辑地址
const onAddressEdit = (item) => {
  uni.navigateTo({ url: "/pages/user/address/add?id=" + item.shopReceiveId });
};
// 删除地址
const onAddressDelete = (item, index) => {
  addressList.value.splice(index, 1);
  studyApi.SHOPRECEIVEGBATCHDELETE({
      id: item.shopReceiveId
    })
    .then((res) => {
      if (res.code == 1) {
        uni.showToast({
          title: "删除成功",
          icon: "success",
          mask: true
        });
      }
    });
};

// 触底加载
onReachBottom(() => {
  console.log("触底加载");
  if (addressList.value.length >= totalCount.value) {
    loadStatus.value = "没有更多数据了";
    return
  }
  loadStatus.value = "正在加载中";
  currentPage.value++
  getData(2);
});
// 下拉刷新
onPullDownRefresh(() => {
  loadStatus.value = "正在加载中";
  addressList.value = [];
  currentPage.value = 1
  totalCount.value = 0
  isloading.value = false
  this.getData(1,() => uni.stopPullDownRefresh())
  setTimeout(() => {
    uni.stopPullDownRefresh();
  }, 300);
});

</script>

<style lang="scss">
page {
  background-color: #ededed;
}
</style>
<style lang="scss" scoped>
.vmall-address {
  .container {
    padding:24rpx 24rpx 100rpx;
    box-sizing: border-box;
    
    .list{
      margin-bottom: 24rpx;
    }
  }
  .moren{
    background: rgba(214, 142, 68, 1);
    font-family: PingFang SC;
    font-size: 12px;
    font-weight: 400;
    line-height: 20px;
    text-align: center;
    text-underline-position: from-font;
    text-decoration-skip-ink: none;
    color: #fff;
    padding: 0 4rpx;
    box-sizing: border-box;
    border-radius: 4rpx;
    margin-left: 10rpx;
  }
  .list {
    width: 100%;
    background: #ffffff;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1rpx solid #e6e6e6;
    box-sizing: border-box;
    .mid{
      width: calc(100% - 150rpx);
    }
  }
  .icon-width {
    width: 40rpx;
    height: 40rpx;
    vertical-align: text-top;
  }
  .empty-container {
    background: #ffffff;
    height: calc(100vh - 84rpx);
  }
  .btn-fixed {
    position: fixed;
    bottom: 0;
    width: 100%;
    margin: 0 auto;
    background: #ffffff;
    box-sizing: border-box;
  }
  .save-btn {
    width: 100%;
    height: 84rpx;
    line-height: 84rpx;
    background: rgba(214, 142, 68, 1);
    color: #ffffff;
    text-align: center;
    border-radius: 30px 30px 30px 30px;
  }
}
</style>
